<?php
/**
 * 类 PDO 接口 
 *
 * @filesource _pdo.class.php
 * @package peck
 * @subpackage _db
 * @version $id: 0.1, utf8, Mon Jan 11 20:52:01 CST 2010
 * @author LD King <kldscs[at]gmail.com>
 * @copyright Copyleft (D.) 2007 - 2010 MiFunny China Inc.
 * @link http://mifunny.info/
 * @example 
 *   
 */
class _pdo extends PDO{
	/**
	 * @var int $query_count 本次连接所执行的SQL语句数目
	 */
	public $query_count = 0;
	
    public function  __construct(array $policy=null){
        if( empty($policy['dsn']) ){
            throw new LogicException('('.__METHOD__.')'.'Error: database DSN information required!');
        }else{
            //$options = array_merge(array('ATTR_PERSISTENT'=>false,), $policy['driver_options']);
            $options = $policy['options'];
             if( is_array($options) ) {
	            foreach($options as $k=>$v){
	                $options[constant('PDO::'.$k)] = $v;
	                unset($options[$k]);
	            }
            }else $options = NULL;
            try {
                parent::__construct($policy['dsn'], $policy['username'], $policy['password'],$options);
			    $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); //setFetchMode fetch模式,关联数组
			    if( !empty($policy['charset'])  ){ //字符集
				    $charset = "SET NAMES ".$policy['charset'];
				    if( !empty($policy['collate']) ){
					    $charset .= " COLLATE ".$policy['collate'];
				    }
				    $this->exec($charset);
			    }
            }catch(PDOException $e){
                throw new RuntimeException('('.__METHOD__.')'.'Database Server Connect Error: '.$e->__toString());
            }
        }
    }
    
    public function query($statement){
        ++ $this->query_count;
        return parent::query($statement);
    }

    public function exec($statement){
        ++ $this->query_count;
        return parent::exec($statement);
    }

}//END interface MagicDB
?>
